package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("getUsername")
    public Result getUsername(Principal principal) {
        try {
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, principal.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }


    @PreAuthorize("hasAnyAuthority('USER_ADD')")
    @RequestMapping("add")
    public Result add(@RequestParam("roleIds") Integer[] roleIds, @RequestBody User user) {
        try {
            userService.add(roleIds, user);
            return new Result(true,"添加用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = userService.findPage(queryPageBean);
            return new Result(true,"查询用户数据成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("findById")
    public Result findById(@RequestParam("id") Integer id) {
        try {
            User user = userService.findById(id);
            return new Result(true,"回显用户数据成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显用户数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_QUERY')")
    @RequestMapping("findRoleIdsByRoleId")
    public Result findRoleIdsByRoleId(@RequestParam("id") Integer id) {
        try {
            List<Integer> roleIds = userService.findRoleIdsByRoleId(id);
            return new Result(true,"获取角色列表数据成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取角色列表数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestParam("roleIds") Integer[] roleIds, @RequestBody User user) {
        try {
            userService.edit(roleIds, user);
            return new Result(true,"编辑用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑用户数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('USER_DELETE')")
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            userService.deleteById(id);
            return new Result(true,"删除用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户数据失败");
        }
    }

    @RequestMapping("getMenuList")
    public Result getMenuList(@RequestParam("userName")String userName) {
        try {
            List menuList = userService.getMenuList(userName);
            return new Result(true,"获取用户菜单成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户菜单失败");
        }
    }


}
